<template>
  <!-- 确认提示框组件 -->
  <div class="tips" :class="config.type" id="tips">
    <div
      class="tips_context"
      :style="{
        top: top + 'px',
        width: config.setWidth + 'px',
        padding: config.padding
      }"
    >
      <p
        class="tips_tips"
        v-if="config.title"
        :style="{
          textAlign: config.titleAlign
        }"
      >
        {{ config.title }}
      </p>
      <!-- 没传入msg就提示 ?? 新语法 -->
      <p
        class="tips_msg"
        :style="{
          textAlign: config.textAlign
        }"
      >
        {{ config.content || '请输入内容' }}
      </p>

      <p
        class="iconfont icon-31guanbi swh-box-off"
        v-if="config.closeBtn"
        @click="close('ciick'), off('ciick')"
      ></p>
    </div>
  </div>
</template>
<script>
import { onMounted, ref } from 'vue';
export default {
  name: 'tips',
  props: {
    config: {
      type: Object
    },
    close: {
      type: Function,
      required: true
    }
  },
  setup(props) {
    onMounted(() => {
      animation();
    });
    const iconlist = ref({
      success: 'icon-trues',
      warning: 'icon-careful-full',
      error: 'icon-guanbi',
      default: 'icon-icoguanyuwomen'
    });
    const defaultTime = 2000;
    const topDistance = 20;
    const top = ref(-100);
    const animation = function () {
      // 延迟改变top才会有动画效果
      setTimeout(() => {
        const myTop = props.config.topDistance || topDistance;
        //  顶部距离最小20px
        top.value = myTop >= topDistance ? myTop : topDistance;
      }, 0);
      // // 设置最短显示时间700毫秒
      let newTime = props.config.time < 700 ? 700 : props.config.time;
      if (props.config.closeBtn) return;
      // 没设置时间就使用默认时间
      off(newTime);
    };
    const off = (newTime) => {
      let time;
      if (newTime == 'ciick') {
        time = 0;
      } else {
        time = newTime || defaultTime;
      }
      setTimeout(() => {
        top.value = -100;
      }, time);
    };
    return {
      iconlist,
      off,
      top,
      animation
    };
  }
};
</script>
<style lang="less" scoped>
/* 外层遮罩层 */
.tips_context {
  width: 300px;
  color: @HSC;
  background: #ffffff;
  box-shadow: 0px 1px 5px rgb(175, 175, 175);
  position: fixed;
  left: 50%;
  transform: translate(-50%);
  border-radius: 5px;
  padding: 10px 20px;
  z-index: 999;
  box-sizing: border-box;
  transition: 0.5s;
  .tips_tips {
    text-align: center;
    line-height: 24px;
    font-weight: 700;
  }
  .tips_msg {
    caret-color: transparent;
    line-height: 20px;
    font-size: 14px;
    text-align: center;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
  }
  .swh-box-off {
    position: absolute;
    caret-color: transparent;
    right: 5px;
    top: 5px;
    font-size: 14px;
    &:hover {
      cursor: pointer;
      opacity: 0.8;
    }
  }
}
.success .tips_context {
  color: #0dc300;
  background: #f4ffef;
}
.warning .tips_context {
  color: #de8500;
  background: #fff8ee;
}
.error .tips_context {
  color: red;
  background: #ffefef;
}
.default .tips_context {
  color: #0062ff;
  background: #ecf8ff;
}
</style>
